package com.han.gift.controller;





import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.han.gift.common.util.AjaxResult;
import com.han.gift.common.util.PageList;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.han.gift.entity.Meal;
import com.han.gift.entity.Meal;
import com.han.gift.query.MealQuery;
import com.han.gift.service.MealService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

@RestController
@RequestMapping("meal")
public class MealController{

    //@Autowired
    //public ${table.serviceName} mealService;

	/**
     * 服务对象
     */
    @Autowired
    private MealService mealService;


    /**
     * 保存和修改公用的
     * @param
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Meal meal){
        try {
            if( meal.getId()!=null)
                mealService.updateById(meal);
            else
                mealService.save(meal);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {

            mealService.removeById(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setMessage("删除对象失败！"+e.getMessage());
        }
    }

    //获取用户
    @GetMapping("/{id}")
    public AjaxResult get(@PathVariable("id")Long id)
    {
        try {

            Meal meal = mealService.getById(id);
            return AjaxResult.me().setResultObj(meal);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取一个失败！"+e.getMessage());
        }
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping
    public AjaxResult list(){

        try {
            List< Meal> list = mealService.list(null);
            return AjaxResult.me().setResultObj(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取所有失败！"+e.getMessage());
        }
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */

    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public AjaxResult json(@RequestBody MealQuery query)
    {
        Page<Meal> page = new Page<>(query.getPage(), query.getRows());
        String keyword = query.getKeyword();
        if (Strings.isNotBlank(keyword)) {
            LambdaQueryWrapper<Meal> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.like(Meal::getMealName, keyword)
                    .or().like(Meal::getMealPrice, keyword);

            page = mealService.page(page, queryWrapper);
            PageList pageList = new PageList<Meal>(page.getTotal(), page.getRecords());
            return AjaxResult.me().setResultObj(pageList);
        }
        page = mealService.page(page);
        PageList pageList = new PageList<Meal>(page.getTotal(), page.getRecords());
        return AjaxResult.me().setResultObj(pageList);
    }


    //批量删除
    @PatchMapping
    public AjaxResult batchDelete(@RequestBody List<Long> ids){

        mealService.removeByIds(ids);

        return AjaxResult.me();

    }

}

